package com.bafa.front.events;

import java.io.InputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;

import com.bafa.jdbc.DBAccess;
import com.bafa.objects.PageBean;
import com.bafa.objects.team.Team;
import com.bafa.services.AdminServices;
import com.bafa.services.RetrieveServices;

public class AddNewTeamsEvent extends Event {

	@Override
	public void doEvent(HttpServletRequest request,
			HttpServletResponse response, PageBean page) {
		
		
//		 Create a factory for disk-based file items
        FileItemFactory factory = new DiskFileItemFactory();

        // Create a new file upload handler
        ServletFileUpload upload = new ServletFileUpload(factory);
		try{
			// Parse the request
			List items = upload.parseRequest(request);

			Iterator iter = items.iterator();
			while (iter.hasNext()) {
				FileItem item = (FileItem) iter.next();
				if (!item.isFormField()) {
					if(item.getName().indexOf("xls")<1){
			               
			        }else{
			        	try{
							InputStream uploadedStream = item.getInputStream();
        					POIFSFileSystem fs = new POIFSFileSystem(uploadedStream);
                 			HSSFWorkbook hssfworkbook = new HSSFWorkbook(fs);
                                                
                            /*******************Get Accounts Sheet*************************/
                            HSSFSheet teams = hssfworkbook.getSheetAt(0);
							
							int rows  = teams.getPhysicalNumberOfRows();
                            HashMap addedTeams = new HashMap();
                            for (int r = 1; r < rows; r++)
                            {
								HSSFRow row   = teams.getRow(r);
	                               String teamName = row.getCell((short)0).getRichStringCellValue().getString();
								   String name1 = row.getCell((short)1).getRichStringCellValue().getString();
								   String email1 = row.getCell((short)2).getRichStringCellValue().getString();
								   String tel1 = "";
								   if(row.getCell((short)3).getCellType()==HSSFCell.CELL_TYPE_NUMERIC)
									   tel1 = new Integer(((int)row.getCell((short)3).getNumericCellValue())).toString();
								   else
									   tel1 = row.getCell((short)3).getRichStringCellValue().getString();
								   String name2 = row.getCell((short)4).getRichStringCellValue().getString();
								   String email2 = row.getCell((short)5).getRichStringCellValue().getString();
								   String tel2 = "";
								   if(row.getCell((short)6).getCellType()==HSSFCell.CELL_TYPE_NUMERIC)
									   tel2 = new Integer(((int)row.getCell((short)6).getNumericCellValue())).toString();
								   else
									   tel2 = row.getCell((short)6).getRichStringCellValue().getString();
								   
								   
								   String shirtColour = row.getCell((short)7).getRichStringCellValue().getString();
								   
								   message.addAllMessages(AdminServices.addNewTeam(teamName,name1,name2,email1,email2,tel1,tel2,shirtColour));
								  
                            }
							
			        	}catch(Exception e){
							e.printStackTrace();
				
			        	}
			        }
				}
			}
		}catch(Exception e){
		
		}

	}

}
